/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.activity.update;

import android.app.ProgressDialog;
import android.content.Context;

import com.dataoke.shoppingguide.app773026.api.remote.JKJApi;
import com.dataoke.shoppingguide.app773026.bean.Version;
import com.dataoke.shoppingguide.app773026.util.DialogHelper;
import com.loopj.android.http.TextHttpResponseHandler;

import cz.msebera.android.httpclient.Header;

/**
 *
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public class CheckUpdateManager {


    private ProgressDialog mWaitDialog;
    private Context mContext;
    private boolean mIsShowDialog;

    public CheckUpdateManager(Context context, boolean showWaitingDialog) {
        this.mContext = context;
        mIsShowDialog = showWaitingDialog;
        if (mIsShowDialog) {
            mWaitDialog = DialogHelper.getProgressDialog(mContext);
            mWaitDialog.setMessage("正在检查中...");
            mWaitDialog.setCancelable(true);
            mWaitDialog.setCanceledOnTouchOutside(true);
        }
    }


    public void checkUpdate(final boolean isHasShow) {
        if (mIsShowDialog) {
            mWaitDialog.show();
        }
        JKJApi.checkUpdate(new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                try {
                    if (mIsShowDialog) {
                        DialogHelper.getMessageDialog(mContext, "网络异常，无法获取新版本信息").show();
                    }
                    if (mWaitDialog != null) {
                        mWaitDialog.dismiss();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
//                    ResultBean<List<Version>> bean = AppOperator.createGson()
//                            .fromJson(responseString, new TypeToken<ResultBean<List<Version>>>() {
//                            }.getType());
//                    if (bean != null && bean.isSuccess()) {
//                        List<Version> versions = bean.getResult();
//                        if (versions.size() > 0) {
//                            final Version version = versions.get(0);
//                            int curVersionCode = BuildConfig.VERSION_CODE;
//                            int code = Integer.parseInt(version.getCode());
//                            if (curVersionCode < code) {
//                                //是否弹出更新
//                                if (JKJSharedPreference.getInstance().isShowUpdate() || isHasShow) {
//                                    UpdateActivity.show((Activity) mContext, version);
//                                }
//                            } else {
//                                if (mIsShowDialog) {
//                                    DialogHelper.getMessageDialog(mContext, "已经是新版本了").show();
//                                }
//                            }
//                        }
//                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();
                if (mWaitDialog != null) {
                    mWaitDialog.dismiss();
                }
            }
        });
    }

    @SuppressWarnings("all")
    public void setCaller(RequestPermissions caller) {
        RequestPermissions mCaller = caller;
    }

    public interface RequestPermissions {
        void call(Version version);
    }
}
